/*********************\
* Author:Dzlua
* QQ:505544956
* Time:2017/04/08
\**********************/
#include "StdAfx.h"
#include "SDConvert.h"

namespace DuiLib {

SDString SDConvert::WToA(SDWString str)
{
    int iTextLen = ::WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, NULL, 0, NULL, NULL);
    char* pElementText = new char[iTextLen + 1];
    memset((void*)pElementText, 0, (iTextLen + 1) * sizeof(char));
    ::WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL);
    SDString strReturn(pElementText);
    delete [] pElementText;
    return strReturn;
}

SDWString SDConvert::AToW(SDString str)
{
    int unicodeLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
    wchar_t* pUnicode = new wchar_t[unicodeLen + 1];
    memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
    ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);
    SDWString strReturn = (wchar_t*)pUnicode;
    delete [] pUnicode;
    return strReturn;
}

SDWString SDConvert::UToW(SDString str)
{
    int unicodeLen = ::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
    wchar_t* pUnicode = new wchar_t[unicodeLen + 1];
    memset((void*)pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
    ::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);
    SDWString strReturn(pUnicode);
    delete [] pUnicode;
    return strReturn;
}

SDString SDConvert::WToU(SDWString str)
{
    int iTextLen = ::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL);
    char* pElementText = new char[iTextLen + 1];
    memset((void*)pElementText, 0, (iTextLen + 1) * sizeof(char));
    ::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL);
    SDString strReturn(pElementText);
    delete [] pElementText;
    return strReturn;
}

SDString SDConvert::AToU(SDString str)
{
    return WToU(AToW(str));
}

SDString SDConvert::UToA(SDString str)
{
    return WToA(UToW(str));
}

SDTString SDConvert::UToT(SDString str)
{
#ifdef _UNICODE
    return UToW(str);
#else
    return UToA(str);
#endif
}

SDTString SDConvert::AToT(SDString str)
{
#ifdef _UNICODE
    return AToW(str);
#else
    return str;
#endif
}

SDTString SDConvert::WToT(SDWString str)
{
#ifdef _UNICODE
    return str;
#else
    return WToA(str);
#endif
}

SDString SDConvert::TToU(SDTString str)
{
#ifdef _UNICODE
    return WToU(str);
#else
    return AToU(str);
#endif
}

SDString SDConvert::TToA(SDTString str)
{  
#ifdef _UNICODE
    return WToA(str);
#else
    return str;
#endif
}

SDWString SDConvert::TToW(SDTString str)
{
#ifdef _UNICODE
    return str;
#else
    return AToW(str);
#endif
}

}